<script setup>
name: 'order'
import { ref, onMounted } from 'vue'
import { getOrdersListAPI, getMockActivity } from '@/api/orderAPI'
import { ElMessage } from 'element-plus';
import moment from 'moment'
import cityData from '@/assets/js/citydata'

const queryInfo = ref({
    query: '',
    pagenum: 1,
    pagesize: 10
})

const ordersList = ref([])
const total = ref(0)
const getOrdersList = async () => {
    const { data: res } = await getOrdersListAPI(queryInfo.value)
    console.log(res);
    if (res.meta.status !== 200) return ElMessage.error(res.meta.msg)
    ordersList.value = res.data.goods
    total.value = res.data.total
}

const handleSizeChange = (val) => {
    queryInfo.value.pagesize = val
    getOrdersList()
}

const handleCurrentChange = (val) => {
    queryInfo.value.pagenum = val
    getOrdersList()
}

// 验证规则
const rules = {
    address1: [
        { required: true, message: '请输入省市区/县', trigger: 'blur' }
    ],
    address2: [
        { required: true, message: '请输入详细地址', trigger: 'blur' }
    ],
}

// 编辑地址
const editDialogVisible = ref(false)
const editForm = ref({
    address1: [],
    address2: ''
})


const editDialog = (row) => {
    editDialogVisible.value = true
}
const editFormRef = ref(null)
const editHandleClose = () => {
    editFormRef.value.resetFields()
    editForm.value = {
        address1: [],
        address2: ''
    }
}
const editOrder = () => {
    ElMessage.success('修改地址成功')
    editDialogVisible.value = false
}

const props = {
    expandTrigger: 'hover',
    value: 'value',
    label: 'label',
    children: 'children'
}
const progressDialogVisible = ref(false)
const activity = ref([])

const mapDialog = async () => {
    const { data: res } = await getMockActivity()
    if (res.meta.status !== 200) return ElMessage.error(res.meta.msg)
    activity.value = res.data
    progressDialogVisible.value = true
}

const progressHandleClose = () =>{
    activity.value = []
}

onMounted(() => {
    getOrdersList()
})

</script>

<template>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
        <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getOrdersList">
            <template #append>
                <el-button icon="Search" @click="getOrdersList" />
            </template>
        </el-input>
        <!-- 表格区域 -->
        <el-table :data="ordersList" stripe border style="width: 100%">
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="order_number" label="订单编号" />
            <el-table-column prop="order_price" label="订单价格" />
            <el-table-column label="是否付款">
                <template v-slot="scope">
                    <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
                    <el-tag type="success" v-else>已付款</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="is_send" label="是否发货" />
            <el-table-column label="下单时间" width="170">
                <template v-slot="scope">
                    {{ moment(scope.row.update_time).format("YYYY-MM-DD HH:mm:ss") }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="130">
                <template v-slot="scope">
                    <el-button type="primary" icon="Edit" @click="editDialog(scope.row)"></el-button>
                    <el-button type="success" icon="MapLocation" @click="mapDialog(scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="queryInfo.pagenum" :page-size="queryInfo.pagesize" background
            :page-sizes="[10, 20, 30, 40]" layout="total, sizes, prev, pager, next, jumper" :total="total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </el-card>
    <!-- 编辑地址对话框 -->
    <el-dialog v-model="editDialogVisible" title="修改地址" width="50%" @closed="editHandleClose">
        <el-form :model="editForm" ref="editFormRef" :rules="rules">
            <el-form-item label="省市区/县" prop="address1">
                <el-cascader v-model="editForm.address1" :options="cityData" :props="props" />
            </el-form-item>
            <el-form-item label="详细地址" prop="address2">
                <el-input v-model="editForm.address2"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="editDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="editOrder">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
    <!-- 显示物流进度对话框 -->
    <el-dialog title="物流进度" v-model="progressDialogVisible" width="50%" @closed="progressHandleClose">
        <el-timeline>
            <el-timeline-item v-for="(activity, index) in activity" :key="index" 
                :timestamp="activity.time">
                {{ activity.context }}
            </el-timeline-item>
        </el-timeline>
    </el-dialog>
</template>

<style scoped></style>